package com.tangyuan.common.util;

import com.tangyuan.common.constant.RedisConstant;
import com.tangyuan.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
@Component
public class CommStatus implements ApplicationContextAware {

    private static RedisTemplate redisTemplate;



    @Autowired
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        CommStatus.redisTemplate = redisTemplate;
    }

    public static User getUser(String token) {
        if(StringUtils.isEmpty(token)){
            return null;
        }
        token = token.replace("Bearer ","").trim();
        return (User)redisTemplate.opsForValue().get(RedisConstant.USER_LOGIN_KEY_PREFIX + token);
    }
    public static String joinToken(String token) {
        return RedisConstant.USER_LOGIN_KEY_PREFIX + token;
    }

    /**
     * @param key
     * @return
     */
    public static Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }
    /**
     *
     * @param key 键
     * @param value 值
     */
    public static void set(String key,Object value) {
        redisTemplate.opsForValue().set(key,value);
    }

    /**
     *
     * @param key 键
     * @param value 值
     * @param time 有效期
     * @param unit 时间单位
     */
    public static void set(String key,Object value,int time,TimeUnit unit) {
        redisTemplate.opsForValue().set(key, value, time, unit);
    }
    /**
     *
     * @param key 键
     */
    public static void delete(String key) {
        redisTemplate.delete(key);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        redisTemplate = applicationContext.getBean("redisTemplate",RedisTemplate.class);
    }

    /**
     * 查看缓存是否存在
     * @param key 键
     * @param value 值
     * @param time 有效期
     * @param unit 时间单位
     */
    public static Object viewCacheExists(String key, Object value, int time, TimeUnit unit) {
        Object object = CommStatus.get(key);
        if(object == null){
            set(key,value,time,unit);
            return null;
        }
        return object;
    }


    public static String getCode(Integer code) {
        Random random = new Random();
        StringBuilder sb = new StringBuilder(code);
        for (int i = 0; i < 7; i++) {
            // 生成一个随机数，判断是生成数字还是大写字母
            int randomNumber = random.nextInt(3);
            char randomChar;
            if (randomNumber == 0) {
                // 生成0-9的随机数字
                randomChar = (char) ('0' + random.nextInt(10));
            } else {
                // 生成A-Z的随机大写字母
                randomChar = (char) ('A' + random.nextInt(26));
            }
            sb.append(randomChar);
        }
        return sb.toString();
    }

    // 正则表达式匹配中国大陆手机号（11位数字，以1开头）
    private static final String CHINA_MOBILE_PATTERN = "^1[3-9]\\d{9}$";

    public static boolean isValidChinaMobileNumber(String number) {
        if (number == null || number.isEmpty()) {
            return false;
        }
        Pattern pattern = Pattern.compile(CHINA_MOBILE_PATTERN);
        Matcher matcher = pattern.matcher(number);
        return matcher.matches();
    }

    public static void main(String[] args) {
        byte[] bytes = {-84, -19, 0, 5, 116, 0, 43, 117, 115, 101, 114, 58, 108, 111, 103, 105, 110, 58, 50, 101, 49, 52, 101, 102, 53, 100, 51, 101, 102, 101, 52, 101, 102, 48, 57, 50, 56, 54, 97, 56, 99, 55, 55, 48, 102, 52, 99, 54, 56, 55};
        // byte[]转String 2024/4/29 下午5:01 By Wesin
        new String(bytes);
    }
}
